package com.lantosec.hmrp.modules.sysdata.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lantosec.hmrp.core.util.UUIDUtils;
import com.lantosec.hmrp.modules.sysdata.entity.SysProcessForm;
import com.lantosec.hmrp.modules.sysdata.mapper.SysProcessFormMapper;
import com.lantosec.hmrp.modules.sysdata.service.ISysProcessFormService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;

/**
 * 流程与表单的关系表 服务
 *
 * @author lichaowei
 * @date 2020/4/27
 */
@Service
@Primary
public class SysProcessFormServiceImpl extends ServiceImpl<SysProcessFormMapper, SysProcessForm> implements ISysProcessFormService {

    @Override
    public boolean isExist(String modelId, String tableId) {
        int count = baseMapper.selectCount(new QueryWrapper<SysProcessForm>()
                .eq(String.valueOf(SysProcessForm.ColName.deployment_id), modelId)
                .eq(String.valueOf(SysProcessForm.ColName.form_table_id), tableId));
        return count > 0;
    }

    @Override
    public SysProcessForm selectByDeploymentId(String deploymentId) {
        return baseMapper.selectOne(new QueryWrapper<SysProcessForm>()
                .eq(String.valueOf(SysProcessForm.ColName.deployment_id), deploymentId));
    }

    @Override
    public SysProcessForm selectByDefinitionId(String definitionId) {
        return baseMapper.selectOne(new QueryWrapper<SysProcessForm>()
                .eq(String.valueOf(SysProcessForm.ColName.procdef_id), definitionId));
    }

    @Override
    public int deleteByDeploymentId(String deploymentId) {
        return baseMapper.delete(new QueryWrapper<SysProcessForm>()
                .eq(String.valueOf(SysProcessForm.ColName.deployment_id), deploymentId));
    }

    @Override
    public boolean save(String deploymentId,String procdefId, String tableId, String layoutId, String taskId, String category
            ,String modelName, String processNameRule) {
        boolean flag = false;
        tableId = StringUtils.isBlank(tableId) ? "" : tableId;
        category = StringUtils.isBlank(category) ? "" : category;
        modelName = StringUtils.isBlank(modelName) ? "" : modelName;
        SysProcessForm dbSysProForm = selectByDeploymentId(deploymentId);
        if (!Objects.isNull(dbSysProForm)) {
            dbSysProForm.setProcessName(modelName);
            dbSysProForm.setProcessType(category);
            dbSysProForm.setDeploymentId(deploymentId);
            dbSysProForm.setProcdefId(procdefId);
            dbSysProForm.setFormTableId(tableId);
            dbSysProForm.setStartLayoutId(layoutId);
            dbSysProForm.setStartTaskId(taskId);
            dbSysProForm.setEditTime(new Date());
            dbSysProForm.setProInsNameRule(processNameRule);
            flag = super.update(dbSysProForm, new UpdateWrapper<SysProcessForm>()
                    .eq(String.valueOf(SysProcessForm.ColName.deployment_id), procdefId));
        } else {
            SysProcessForm processForm = new SysProcessForm();
            String uuid = UUIDUtils.get();
            processForm.setProcessFormId(uuid);
            processForm.setProcessName(modelName);
            processForm.setProcessType(category);
            processForm.setDeploymentId(deploymentId);
            processForm.setProcdefId(procdefId);
            processForm.setFormTableId(tableId);
            processForm.setStartLayoutId(layoutId);
            processForm.setStartTaskId(taskId);
            processForm.setCreateTime(new Date());
            processForm.setProInsNameRule(processNameRule);
            flag = super.save(processForm);
        }
        return flag;
    }
}
